package com.hmsg.health.controller.admin;


import com.hmsg.health.bean.Dto.UmengExtra;
import com.hmsg.health.bean.Dto.WithDrawDtos;
import com.hmsg.health.bean.Dto.WithdrawDto;
import com.hmsg.health.bean.domain.AppealDo;
import com.hmsg.health.bean.domain.BannerDo;
import com.hmsg.health.bean.domain.UserDo;
import com.hmsg.health.bean.domain.WithdrawDo;
import com.hmsg.health.bean.enums.StatusCode;
import com.hmsg.health.bean.vo.AppealReqVo;
import com.hmsg.health.bean.vo.WithDrawReqVo;
import com.hmsg.health.dao.AppealDao;
import com.hmsg.health.dao.UserDao;
import com.hmsg.health.dao.WithdrawDao;
import com.hmsg.health.param.ResponseResult;
import com.hmsg.health.security.IgnoreSecurity;
import com.hmsg.health.security.OpenApi;
import com.hmsg.health.service.admin.AdminWithDrawService;
import com.hmsg.health.utils.umeng.push.Demo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Api(value = "运营系统提现记录相关", tags = { "运营系统提现记录相关" })
@Slf4j
@Controller
@RequestMapping("/withdraw")
public class AdminWithDrawController {

    @Autowired
    private AdminWithDrawService adminWithDrawService;
    @Autowired
    private WithdrawDao withdrawDao;
    @Autowired
    private AppealDao appealDao;
    
    @Autowired
    private UserDao userDao;

    //0 待审核 1审核通过 2审核拒绝
    @GetMapping("/list")
    @ApiOperation(value="运营系统提现记录相关", notes="运营系统提现记录相关" ,httpMethod="GET")
    @ResponseBody
    @IgnoreSecurity
    public ResponseResult<WithDrawDtos> queryList(@RequestParam(value = "state",required = false) Integer state,
                                                    @RequestParam(value = "phone",required = false) String phone,
                                                    @RequestParam(value = "time", required = false) String time,
                                                    @RequestParam(value = "pageNo") Integer pageNo,
                                                   @RequestParam(value = "pageSize") Integer pageSize) {
        Integer firstResult = (pageNo-1)*pageSize;
        Integer maxResult =pageSize;

        int totalpages;//总页数

        int total=withdrawDao.countAll(state,phone,time);
        totalpages=   total/pageSize;
        if(total% pageSize !=0){
            totalpages++;
        }

        List<WithdrawDto> withdraws= adminWithDrawService.findAll(state,phone,time,firstResult,maxResult);
        WithDrawDtos withdrawDto = new WithDrawDtos();
        if(withdraws != null && withdraws.size()>0) {
            withdrawDto.setTotalpages(totalpages);
            withdrawDto.setWithdrawDoList(withdraws);
            return new ResponseResult<WithDrawDtos>(StatusCode.SYSTEM_SUCCESS, withdrawDto);
        }
        return new ResponseResult<WithDrawDtos>(StatusCode.SYSTEM_SUCCESS, withdrawDto);
    }


    //去审核  审核失败  还给账户金豆
    @PostMapping("/verify")
    @ApiOperation(value="审核", notes="审核" ,httpMethod="POST")
    @ResponseBody
    @IgnoreSecurity
    @OpenApi
    public ResponseResult<Object> editSecond( @ApiParam(name = "审核", value = "传入json格式", required = true)
                                              @RequestBody @Validated WithDrawReqVo vo) throws Exception {

        WithdrawDo one = withdrawDao.findOne(vo.getId());
        if(one != null){
            if(vo.getIsState() ==1) {
                WithdrawDo withdrawDo = new WithdrawDo();
                withdrawDo.setStatus(vo.getIsState());
                withdrawDo.setId(vo.getId());
                withdrawDao.update(withdrawDo);
            	UserDo user = userDao.select(one.getUserId());
				if(user.getDeviceToken()!=null&&user.getDeviceToken().length()==64) {
					Demo demo = new Demo();
					
					List<UmengExtra> list = new ArrayList<UmengExtra>();
					UmengExtra ue = new UmengExtra();
					ue.setKey("type");
					ue.setValue(7);
					list.add(ue);
					UmengExtra ue1 = new UmengExtra();
					ue1.setKey("resourceId");
					ue1.setValue(one.getId());
					list.add(ue1);
					String text = "恭喜您！您的提现申请已通过，"+one.getAmount()+"元已发放至您的支付宝账户，请查收。";
					
					JSONObject alert = new JSONObject();
					alert.put("title", "提现成功");
					//alert.put("subtitle", "审核拒绝通知");
					alert.put("body", text);
					demo.sendIOSUnicast(user.getDeviceToken(), alert,list);
					
				}
				
				if(user.getDeviceToken()!=null&&user.getDeviceToken().length()==44) {
					Demo demo = new Demo();
					List<UmengExtra> list = new ArrayList<UmengExtra>();
					UmengExtra ue = new UmengExtra();
					ue.setKey("type");
					ue.setValue(7);
					list.add(ue);
					UmengExtra ue1 = new UmengExtra();
					ue1.setKey("resourceId");
					ue1.setValue(one.getId());
					list.add(ue1);
					String text = "恭喜您！您的提现申请已通过，"+one.getAmount()+"元已发放至您的支付宝账户，请查收。";
					
					
					demo.sendAndroidUnicast(user.getDeviceToken(), "提现成功","提现成功",text, list);
					
				}
                
                return new ResponseResult<Object>(StatusCode.SYSTEM_SUCCESS, null);
            }
            if(vo.getIsState() == 2){
                WithdrawDo withdrawDo = new WithdrawDo();
                withdrawDo.setStatus(vo.getIsState());
                withdrawDo.setId(vo.getId());
                adminWithDrawService.updateAll(withdrawDo);
            }
        }
        return new ResponseResult<Object>(StatusCode.SYSTEM_SUCCESS, null);
    }




}
